<?php

// Created by Khanh Nam
class JobsController extends Controller{
        public $layout = '//layouts/customer';
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions				
             'users'=>array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    public function actionIndex($locno){
        $custno = Yii::app()->user->custno;
        $jobs=new Jobs();
        $jobs->custno=$custno;
        $jobs->locno=$locno;
        $location=Locations::model()->find('custno=:custno AND locno=:locno',array(
            'custno'=>$custno,
            'locno'=>$locno
        ));
        $customer=Customers::model()->findByPk($custno);
        if($location==NULL) throw new CHttpException ('Your request page can not found', 404);
        
        //adding some javascript variable;
        //jsVariableGen('Locations', 'id', array('locname','add1','city','state'), 'locations', 'custno=:custno',array('custno'=>$custno),' | ');
        outputJsByList($jobs->getBillToList(), 'locations');
      
        outputJsByList(SalesDepartments::model()->getListData(), 'departments');
     
        outputJsByList(Employees::model()->getEmployeeList(), 'employees');
        outputJsByList(Employees::model()->getSalePersonList(), 'sale');
     
        outputJsByList(JobsBillingStatus::getListData(), 'billingstatus');
     
        outputJsByList(JobsStatus::getListData(), 'jobsstatus');
     
        outputJsByList(JobsClass::getList(), 'jobsclass');
        outputJsVarible($customer->salesperson, 'defaultSalesPerson');
        
        $this->render('index',array(
            'location'=>$location,
            'jobs'=>$jobs
            ));
    }
    
    public function actionUpdate($jobid){
        
       
        $model=Jobs::model()->findByPk($jobid);
        if($model!=NULL && isset($_POST['Jobs'])){
            $model->attributes=$_POST['Jobs'];
            if($model->save()){
                JobsNotes::model()->updateAll(array('status'=>1),'jobid=:id AND status=-1',array('id'=>$model->jobid));
                JobsDocuments::model()->updateAll(array('status'=>1),'jobid=:id AND status=-1',array('id'=>$model->jobid));
                //$this->redirect(array('index','custno'=>$model->custno,'locno'=>$model->locno));
                echo true;
                Yii::app()->end();
            }
            
            //we call this function so we terminate app here
            
        }
        $this->render('update',array('model'=>$model));
       
    }
  
    protected function performAjaxValidation($model)
    {
            if(isset($_POST['ajax']) && $_POST['ajax']==='jobs-form')
            {
                    echo CActiveForm::validate($model);
                    Yii::app()->end();
            }
    }
    public function actionCancel($jobid){
        $model=Jobs::model()->findByPk($jobid);
        if($model->status==-1){
            $model->delete();
        }else{
            Yii::app()->db->createCommand('delete from tbl_jobs_notes where status=-1 AND jobid=:id')->bindValue('id', $jobid)->query();
            Yii::app()->db->createCommand('delete from tbl_jobs_documents where status=-1 AND jobid=:id')->bindValue('id', $jobid)->query();                        
        }
        $this->redirect(array('index','custno'=>$model->custno,'locno'=>$model->locno));
    }
    public function actionList($custno,$locno,$page=0,$sort=NULL){
        if($page>0) $page--;
            $order=CJSON::decode($sort);
         
            $data=new CActiveDataProvider('Jobs',array(
                'criteria'=>array(
                    'condition'=>'custno=:custno AND locno=:locno',
                    'params'=>array(
                        'custno'=>$custno,
                        'locno'=>$locno,
                    ),
                    'order'=>isset($order)?$order[0]['property'].' '.$order[0]['direction']:NULL
                ),
                'pagination'=>array(
                    'pageSize'=>20,
                    'currentPage'=>$page
                )
            ));
            
            if($data!=NULL && count($data->getData())>0){
                $rs=array();
                $rs['totalCount']=$data->getTotalItemCount();
                foreach($data->getData() as $i=>$item){
                                       
                    $rs['data'][$i]=$item->getAttributes();
                    $rs['data'][$i]['locname']=$item->location->locname;
                    $rs['data'][$i]['billingstatustext']= JobsBillingStatus::getText($item->billingstatus);
                    $rs['data'][$i]['jobstatustext']=JobsStatus::getText($item->jobstatus);
                    
                }
                echo json_encode($rs);
            }
            Yii::app()->end();
    }
    
    public function actionGetLocation($locid){
        $location=Locations::model()->findByPk($locid);
        if($location!=NULL) echo json_encode($location->getAttributes());
        Yii::app()->end();
    }
    
}
?>
